import  AluGeoUtil  from '@/model/geometry/utils/AluGeoUtil';
import { Quaternion, Vector3 } from 'three';
import { HoleMeta, CreateMeta } from '../../meta/MetaClass';
import AModelFmulaUtils from '@/model/utils/AModelFmulaUtils';



export default class LALAluEntity {

    constructor() {

    }


    //模型构件
    static __create(data: AIBasicParams, type: number = 0): GeometryReturnMeta<Array<any>> {
        let vec = [];
        let indices = [];

        //长2高
        let aa = this.__topCom(data);
        vec.push(...aa.vertices);
        indices.push(...aa.indices);

        // //长2宽
        let bb = this.__xoyCom(data);
        indices.push(...bb.indices.map((value) => { value += vec.length / 3; return value; }));
        vec.push(...bb.vertices);

        // //长1高
        let cc = this.__yozTCom(data);
        indices.push(...cc.indices.map((value) => { value += vec.length / 3; return value; }));
        vec.push(...cc.vertices);

        //长1宽
        let dd = this.__xoyVCom(data);
        indices.push(...dd.indices.map((value) => { value += vec.length / 3; return value; }));
        vec.push(...dd.vertices);


        //位移一个
        if (type == 0) {
            let r = 0.5 * Math.PI;
            let q = new Quaternion();
            q.setFromAxisAngle(new Vector3(0, 1, 0), r);
            let v = new Vector3();
            for (let i: number = 0; i < vec.length; i += 3) {
                let x = vec[i];
                let y = vec[i + 1];
                let z = vec[i + 2];

                x -= (+data.L2);
                y -= (+data.L1);

                v.set(x, y, z).applyQuaternion(q);

                vec[i] = v.x;
                vec[i + 1] = v.y;
                vec[i + 2] = v.z;
            }
        }
        else if (type == 1) {
            let rr = 0.5 * Math.PI;
            let qq = new Quaternion();
            qq.setFromAxisAngle(new Vector3(1, 0, 0), rr);
            let vv = new Vector3();
            for (let i: number = 0; i < vec.length; i += 3) {
                let x = vec[i];
                let y = vec[i + 1];
                let z = vec[i + 2];

                x -= (+data.L2);
                y -= (+data.L1);

                vv.set(x, y, z).applyQuaternion(qq);

                vec[i] = vv.x;
                vec[i + 1] = vv.y;
                vec[i + 2] = vv.z;
            }
        }


        this.setDuan(data, type);

        return {
            vertices: vec,
            indices: indices,
            meshface: []
        }
    }

    private static setDuan(data: AIBasicParams, type: number = 0) {

        if (type == 0) {
            data["duan"] = [
                0, 0, 0,
                0, -data.L1, 0,
                0, 0, data.L2
            ];
            data["zhong"] = {
                L1: [
                    {
                        pointer: [data.H / 2, -data.L1, 0],
                        center: [data.H / 2, -data.L1 / 2, 0]
                    },
                ],
                L2: [
                    {
                        pointer: [0, -data.W2 / 2, data.L2],
                        center: [0, -data.W2 / 2, data.L2 / 2]
                    },
                ]
            }
        }
        else if (type == 1) {
            data["duan"] = [
                0, 0, 0,
                0, 0, -data.L1,
                -data.L2, 0, 0 
            ];
            data["zhong"] = {
                L1: [
                    {
                        pointer: [-data.W1 / 2, 0, -data.L1],
                        center: [-data.W1 / 2, 0, -data.L1 / 2]
                    },
                ],
                L2: [
                    {
                        pointer: [-data.L2, 0, -data.W2 / 2],
                        center: [-data.L2 / 2, 0 , -data.W2 / 2]
                    },
                ]
            }
        }


    }

    /** top板
     *  l = y
     *  w = x
     *  t = z
    */
    private static __topCom(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let l = +data.H, w = +data.L2, t = +data.bkT, l2 = +data.L1;
        let bkd = (+data.BKDia) * 0.5;
        let b_dis = +data["p_c2gkbj"];
        let r = (+data["angle1"] * -1);

        //与zoy板的交接处理
        let hr = (r * 0.5) / 180 * Math.PI;
        let zyr = Math.tan(hr);
        let zw = zyr == 0 ? 0 : t / zyr;
        zw *= -1;


        let back = [
            0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        ];

        let front = [
            0, t, t, w - zw, t, t, w - zw, l, t, 0, l, t,
        ];

        // AluGeoUtil.filterHole(w - bkd, AModelFmulaUtils.getHolesbyExpress(data["p_c2gk"],0))
        // data["p_c2gk"] = data["o_p_c2gk"];

        let holes = AModelFmulaUtils.getHolesbyExpress(data["p_c2gk"], w, 2, data, "p_c2gk");
        holes = AluGeoUtil.reverseHole(w, holes);

        let h = new HoleMeta(bkd, l - b_dis, holes, "x");
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);

        //绕x旋转90度
        let xr = 0.5 * Math.PI;
        let sr = Math.sin(xr);
        let cr = Math.cos(xr);
        for (let i: number = 0; i < result.vertices.length; i += 3) {
            let y = result.vertices[i + 1];
            let z = result.vertices[i + 2];

            result.vertices[i + 1] = y * cr - sr * z + l2;
            result.vertices[i + 2] = sr * y + z * cr;
        }

        return result;
    }

    /** x向xoy竖版
     *  l = y
     *  w = x
     *  t = z
     *  offY = l1
     *  w1 = 侧边xoy板的宽
    */
    private static __xoyCom(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let l = +data.W2, w = +data.L2, t = +data.bkT, offY = +data.L1, w1 = +data.W1;
        let bkd = (+data.BKDia) * 0.5;
        let b_dis = +data["p_c2kkbj"];
        let r = (+data["angle1"]);


        //与zoy板的交接处理
        let hr = (r * 0.5) / 180 * Math.PI;
        let zyr = Math.tan(hr);
        let zw = zyr == 0 ? 0 : t / zyr;

        //与xoyV交接的处理
        let rr = r / 180 * Math.PI;
        let tr = Math.tan(rr);
        let sr = Math.sin(rr);
        let ow = w1 / sr + (Math.abs(r) == 90 ? 0 : l / tr);

        let back = [
            0, 0, 0, w - ow, 0, 0, w, l, 0, 0, l, 0,
        ];
        let front = [
            0, 0, t, w - ow, 0, t, w - zw, l - t, t, 0, l - t, t,
        ];

        let holes = AModelFmulaUtils.getHolesbyExpress(data["p_c2kk"], w);
        let filterHole = AluGeoUtil.filterHoleLess(ow + bkd, holes);
        holes = AModelFmulaUtils.getHolesbyExpress(data["p_c2kk"], w, 2, data, "p_c2kk", Math.abs(filterHole.length - holes.length));
        holes = AluGeoUtil.reverseHole(w, holes);

        let h = new HoleMeta(bkd, b_dis, holes, "x");
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);

        for (let i: number = 0; i < result.vertices.length; i += 3) {
            result.vertices[i + 1] += (offY - l);
        }

        return result;
    }

    /** y向yoz面竖版*/
    private static __yozTCom(data: AIBasicParams): GeometryReturnMeta<Array<any>> {
        let l = +data.L1, w = +data.H, t = +data.bkT, l2 = +data.L2;
        let bkd = (+data.BKDia) * 0.5;
        let b_dis = +data["p_c1gkbj"];
        let r = (+data["angle1"] * -1);


        //与zoy板的交接处理
        let hr = (r * 0.5) / 180 * Math.PI;
        let zyr = Math.tan(hr);
        let zw = zyr == 0 ? 0 : t / zyr;
        zw *= -1;

        //绕y
        let yr = -90 / 180 * Math.PI;
        let ycr = Math.cos(yr);
        let ysr = Math.sin(yr);

        //绕z
        let mr = -90 - r;
        let rr = mr / 180 * Math.PI;
        let v = new Vector3(0, l, 0);
        let q = new Quaternion();
        q.setFromAxisAngle(new Vector3(0, 0, 1), rr);
        v.applyQuaternion(q);         //偏移量
        let ox = v.x;
        let oy = v.y - l;

        // let ww = tr == 0 ? 0 : t / tr
        let ww = 0;

        let back = [
            ww, 0, 0, w, 0, 0, w, l, 0, ww, l, 0,
        ];

        let front = [
            t, 0, t, w, 0, t, w, l - zw, t, t, l - zw, t,
        ];

        // AluGeoUtil.filterHole(l, AModelFmulaUtils.getHolesbyExpress(data["p_c1gk"],0))
        // AluGeoUtil.reverseHole(
        // data["p_c1gk"] = data["o_p_c1gk"];

        let holes = AModelFmulaUtils.getHolesbyExpress(data["p_c1gk"], l, 2, data, "p_c1gk");
        holes = AluGeoUtil.reverseHole(l, holes);


        let h = new HoleMeta(bkd, w - b_dis, holes);
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);


        for (let i: number = 0; i < result.vertices.length; i += 3) {
            let x = result.vertices[i];
            let y = result.vertices[i + 1];
            let z = result.vertices[i + 2];

            let xcr = x * ycr;
            let xsr = x * ysr;
            let zcr = z * ycr;
            let zsr = z * ysr;

            x = xcr + zsr;
            z = zcr - xsr;

            v.set(x, y, z).applyQuaternion(q);

            result.vertices[i] = v.x - ox + l2;
            result.vertices[i + 1] = v.y - oy;
            result.vertices[i + 2] = v.z;

        }

        return result;
    }


    /** y向xoy面竖版*/
    private static __xoyVCom(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let w = +data.L1, l = +data.W1, t = +data.bkT, offY = +data.L2, w1 = +data.W2;
        let bkd = (+data.BKDia) * 0.5;
        let b_dis = +data["p_c1kkbj"];
        let r = +data["angle1"];


        //与xoz板的交接处理
        let hr = (r * 0.5) / 180 * Math.PI;
        let zyr = Math.tan(hr);
        let zw = zyr == 0 ? 0 : t / zyr;
        // zw = 0
        // let zw = t;

        //旋转
        let rr = r / 180 * Math.PI;
        let tr = Math.tan(rr);
        let sr = Math.sin(rr);
        let ow = w1 / sr + (Math.abs(r) == 90 ? 0 : l / tr);

        //与x向zoy交接处理
        let xx_o = t;

        let back = [
            -w, 0, 0, 0, 0, 0, -ow, l, 0, -w, l, 0,
        ];
        let front = [
            -w, xx_o, t, -zw, xx_o, t, -ow, l, t, -w, l, t,
        ];


        //过滤的是前面的几个， 这个表达式第二次其实已经过滤了前面的
        let holes = AModelFmulaUtils.getHolesbyExpress(data["p_c1kk"], w);
        let filterHole = AluGeoUtil.filterHoleLess(ow + bkd, holes);
        holes = AModelFmulaUtils.getHolesbyExpress(data["p_c1kk"], w, 2, data, "p_c1kk", Math.abs(filterHole.length - holes.length));
        holes = AluGeoUtil.reverseHole(w, holes);


        // let hh = AluGeoUtil.filterHole(w - ow, AModelFmulaUtils.getHolesbyExpress(data["p_c1kk"],0), bkd);


        let h = new HoleMeta(bkd, l - b_dis, holes.map((value) => {
            value -= w;
            return value;
        }), "x");
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);


        let q = new Quaternion();
        let v = new Vector3();
        q.setFromAxisAngle(new Vector3(0, 0, 1), rr);

        for (let i: number = 0; i < result.vertices.length; i += 3) {

            let x = result.vertices[i];
            let y = result.vertices[i + 1];
            let z = result.vertices[i + 2];

            v.set(x, y, z).applyQuaternion(q);

            result.vertices[i] = v.x + offY;
            result.vertices[i + 1] = v.y + w;
            result.vertices[i + 2] = v.z;
        }

        return result;
    }
}
